package structure.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author dali
 * @date 2021-07-20 20:51
 * @Description jdk动态代理
 */

public class UserJDKProxy {
    private IUserDao userDao;

    public UserJDKProxy(IUserDao userDao) {
        this.userDao = userDao;
    }

    public Object getProxyInstance(){
        return Proxy.newProxyInstance(
                userDao.getClass().getClassLoader(),
                userDao.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("使用jdk代理");
                        Object invoke = method.invoke(userDao, args);
                        return invoke;
                    }
                }
        );
    }

}
